package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapp.entity.UserInfo;

import java.lang.ref.PhantomReference;
import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox checkbox;
    private boolean is_login;

    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user",MODE_PRIVATE);


        //是否记住了密码
        boolean isLogin = mSharedPreferences.getBoolean("is_login", false);
        String username = mSharedPreferences.getString("username",null);
        String password = mSharedPreferences.getString("password",null);

//        Toast.makeText(this, Boolean.toString(isLogin), Toast.LENGTH_SHORT).show();
        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkbox = findViewById(R.id.checkbox);

//        if(isLogin){
            et_username.setText(username);
            et_password.setText(password);
            checkbox.setChecked(isLogin);
//        }



        //点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到注册页面
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });


        //登录
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if(TextUtils.isEmpty(username) && TextUtils.isEmpty(password)){
                    Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{

                    UserInfo login = new UserInfo(username,password);

                    //查询用户是否存在
                    LCQuery<LCObject> row = new LCQuery<>("UserTable");
                    row.whereEqualTo("username", username);
                    row.countInBackground().subscribe(new Observer<Integer>() {
                        public void onSubscribe(Disposable disposable) {}
                        public void onNext(Integer count) {
                            if(count!=1){
                                Toast.makeText(LoginActivity.this, "用户未注册", Toast.LENGTH_SHORT).show();
                            }
                            }
                            public void onError(Throwable throwable) {}
                            public void onComplete() {}
                        });

                    LCQuery<LCObject> query = new LCQuery<>("UserTable");
                    query.whereEqualTo("username", username);
                    query.getFirstInBackground().subscribe(new Observer<LCObject>() {
                        public void onSubscribe(Disposable disposable) {}
                        public void onNext(LCObject todo) {

                            if (todo != null) {
                                //获取查询到的用户名uname和密码pwd
                                String uname = todo.getString("username");
                                String pwd = todo.getString("password");

                                if (username.equals(uname) && password.equals(pwd)) {
                                    //保存是否勾选了记住密码
                                    SharedPreferences.Editor edit = mSharedPreferences.edit();
                                    edit.putBoolean("is_login", is_login);
                                    if (checkbox.isChecked()) {
                                        edit.putString("username", username);
                                        edit.putString("password", password);
                                    } else {
                                        edit.remove("username");
                                        edit.remove("password");
                                        edit.remove("is_login");
                                    }
                                    edit.commit();
                                    //登录成功
                                    UserInfo.setUserInfo(login);
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                        public void onError(Throwable throwable) {
                            Toast.makeText(LoginActivity.this, "连接错误，请稍后再试", Toast.LENGTH_SHORT).show();
                        }
                        public void onComplete() {}
                    });
                }
            }
        });



        //登录
//        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String username = et_username.getText().toString();
//                String password = et_password.getText().toString();
//                if(TextUtils.isEmpty(username) && TextUtils.isEmpty(password)){
//                    Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
//                }else{
//                    UserInfo login = UserDbHelper.getInstance(LoginActivity.this).login(username);
//
//
//                    LCQuery<LCObject> query = new LCQuery<>("Todo");
//                    query.whereEqualTo("username",username);
//                    query.getFirstInBackground().subscribe(new Observer<LCObject>() {
//                        public void onSubscribe(Disposable disposable) {}
//                        public void onNext(LCObject todo) {
//                            uname = todo.getString("username"); // √
//                            pwd = todo.getString("password"); // √
//                        }
//                        public void onError(Throwable throwable) {}
//                        public void onComplete() {}
//                    });
//
//
//                    Toast.makeText(LoginActivity.this, "获取的云端用户名为"+uname, Toast.LENGTH_SHORT).show();
//
//                    if(username.equals(uname) && password.equals(pwd)){
//                        Toast.makeText(LoginActivity.this, "云端验证通过", Toast.LENGTH_SHORT).show();
//                    }
//
//                    if(login!=null){
//                        if(username.equals(login.getUsername()) && password.equals(login.getPassword()) && username.equals(uname) && password.equals(pwd)){
//
//                            //保存是否勾选了记住密码
//                            SharedPreferences.Editor edit = mSharedPreferences.edit();
//                            edit.putBoolean("is_login",is_login);
//                            if (checkbox.isChecked()) {
//                                edit.putString("username", username);
//                                edit.putString("password", password);
//                            } else {
//                                edit.remove("username");
//                                edit.remove("password");
//                                edit.remove("is_login");
//                            }
//                            edit.commit();
//                            //登录成功
//                            UserInfo.setUserInfo(login);
//                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//                            startActivity(intent);
//                            finish();
//                        }else{
//                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
//                        }
//                    }else{
//                        Toast.makeText(LoginActivity.this, "账号未注册", Toast.LENGTH_SHORT).show();
//                    }
//
//                }
//            }
//        });

        //checkbox的点击事件
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                is_login = isChecked;
            }
        });
    }
}